Use case: MPU configuration

Note

This configuration is applicable with ARMv8-M architectures or above.

These sequences demonstrate the change in the MPU configuration process.

The change is in the ConfigMemoryAttributes function level.

In HAL1, the type of the memory is passed inside the function.

In HAL2, ConfigMemoryAttributes has been split into two separate functions, each one is associated to a specific memory type (device or cache).

Sequence diagram

MPU configuration (ARMv8-M and above)

  @startuml

  group Configure MPU
  "User Application"->"Cortex Driver" : HAL_MPU_Disable()
  group Configure Memory
  "User Application"->"Cortex Driver" : HAL_MPU_ConfigMemoryAttributes()
  "User Application"->"Cortex Driver" : HAL_MPU_ConfigRegion()
  "Cortex Driver"-->"User Application"
  "User Application"->"Cortex Driver" : HAL_MPU_EnableRegion()
  end
  "User Application"->"Cortex Driver" : HAL_MPU_Enable()
  end

  @enduml

Code snippet

This use case demonstrates how to configure a region of the memory (RAM for example) with read/write access with MPU using HAL1 compared to HAL2.

HAL1

HAL2

MPU_Region_InitTypeDef MPU_InitStruct;
MPU_Attributes_InitTypeDef   MPU_AttributesInit;

/* Disable MPU */
HAL_MPU_Disable();

/* Define cacheable memory via MPU */
MPU_AttributesInit.Number             = MPU_ATTRIBUTES_NUMBER1;
MPU_AttributesInit.Attributes         = MPU_NOT_CACHEABLE;
HAL_MPU_ConfigMemoryAttributes(&MPU_AttributesInit);

/* Configure RAM region as Region Number 0, 256KB of size
   and R/W region */
MPU_InitStruct.Enable           = MPU_REGION_ENABLE;
MPU_InitStruct.BaseAddress      = EXAMPLE_RAM_ADDRESS_START;
MPU_InitStruct.LimitAddress     = EXAMPLE_RAM_ADDRESS_START
                                  + EXAMPLE_RAM_SIZE - 1;
MPU_InitStruct.AccessPermission = MPU_REGION_ALL_RW;
MPU_InitStruct.AttributesIndex  = MPU_ATTRIBUTES_NUMBER1;
MPU_InitStruct.IsShareable      = MPU_ACCESS_NOT_SHAREABLE;
MPU_InitStruct.Number           = MPU_REGION_NUMBER0;
MPU_InitStruct.DisableExec      = MPU_INSTRUCTION_ACCESS_ENABLE;

HAL_MPU_ConfigRegion(&MPU_InitStruct);

HAL_MPU_EnableRegion(MPU_REGION_NUMBER0);

HAL_MPU_Enable();
hal_cortex_mpu_region_config_t set_region;

/* Disable MPU before preloading and config update */
HAL_CORTEX_MPU_Disable();

/* Define cacheable memory via MPU */
HAL_CORTEX_MPU_SetDeviceMemAttr(HAL_CORTEX_MPU_MEM_ATTR_1,
                                HAL_CORTEX_MPU_NORMAL_MEM_NCACHEABLE);

/* Configure RAM region as Region Number 0, 256KB of size
   and R/W region */
set_region.base_addr   = EXAMPLE_RAM_ADDRESS_START;
set_region.limit_addr  = EXAMPLE_RAM_ADDRESS_START
                         + EXAMPLE_RAM_SIZE - 1;
set_region.access_attr = HAL_CORTEX_MPU_REGION_ALL_RW;
set_region.exec_attr   = HAL_CORTEX_MPU_EXECUTION_ATTR_ENABLE;
set_region.attr_idx    = ARRAY_ATTRIBUTE_NUMBER;

hal_status_t state = HAL_CORTEX_MPU_SetConfigRegion(HAL_CORTEX_MPU_REGION_0,
                                                    &set_region);
HAL_CORTEX_MPU_EnableRegion(HAL_CORTEX_MPU_REGION_0);

HAL_CORTEX_MPU_Enable();

Key changes

  1. In HAL2, MPU APIs are renamed with the HAL_CORTEX_ prefix.

  2. In HAL2, memory attributes configuration is split into dedicated APIs per memory type.